//
//  QuizHeadScrollView.m
//  SpreadViewDemo
//
//  Created by Tianlong on 2018/1/23.
//  Copyright © 2018年 Tianlong. All rights reserved.
//

#import "QuizHeadScrollView.h"

#define MaxRowH 18
#define FontSize 14
#define MaxViewH 500

@interface QuizHeadScrollView ()

@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentHeightC;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollBottomC;


// 记录scroll底间距移动的距离
@property (assign, nonatomic) CGFloat scBottomMoveDistance;

@property (weak, nonatomic) IBOutlet UILabel *descL;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *descLeftC;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *descRightC;
@property (assign, nonatomic) CGFloat descNormalH;

// 展开按钮
@property (weak, nonatomic) IBOutlet UIButton *spreadBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *spreadBottomC;

@property (assign, nonatomic) BOOL isSpread;
// 已经记录的值不在重复记录
@property (assign, nonatomic) BOOL isRecorded;
@property (assign, nonatomic) CGFloat textRowH;
@property (assign, nonatomic) CGFloat selfNormalH;
@end

@implementation QuizHeadScrollView

+ (instancetype)headViewWithQuestionStyle:(QuizQuestionStyle)style textRowH:(CGFloat)textRowH
{
    QuizHeadScrollView *head = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([QuizHeadScrollView class]) owner:self options:nil] lastObject];
    head.isSpread               = NO;
    head.scBottomMoveDistance   = 0;
    head.clipsToBounds          = YES;
    head.backgroundColor        = [UIColor redColor];
    head.descL.contentMode      = UIViewContentModeTop;//配合label增长动画时,文字不要从中间的内容开始显示.
    head.textRowH               = textRowH;
    switch (style) {
        case QuizQuestionStyleChoice:
            head.descLeftC.constant     = 40;
            head.descRightC.constant    = 40;
            break;
            
        case QuizQuestionStyleMaterials:
            head.descLeftC.constant     = 0;
            head.descRightC.constant    = 0;
            break;
            
        default:
            break;
    }
    [head setupLabel];      // 重新设置下label
    
    return head;
}

#pragma mark - 设置下label的段落样式
- (void)setupLabel {
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    //paragraphStyle.lineHeightMultiple = 20.f;
    //最大行高
    paragraphStyle.maximumLineHeight = self.textRowH>0?self.textRowH:MaxRowH;
    //最小行高
    paragraphStyle.minimumLineHeight = self.textRowH>0?self.textRowH:MaxRowH;
    paragraphStyle.lineSpacing  = 0;
    paragraphStyle.alignment    = NSTextAlignmentJustified;
    NSDictionary *attributes    = @{
                                    NSParagraphStyleAttributeName:paragraphStyle,
                                    //NSForegroundColorAttributeName:[UIColor whiteColor],
                                    NSFontAttributeName:[UIFont systemFontOfSize:FontSize],
                                    NSKernAttributeName:@3//字间距
                                    };
    self.descL.attributedText    = [[NSAttributedString alloc] initWithString:self.descL.text attributes:attributes];
    self.descL.lineBreakMode     = NSLineBreakByTruncatingTail;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (IBAction)clickedSpreadBtn:(id)sender {
    
    __weak typeof(self)ws = self;
    
    if(!self.isRecorded){
        self.isRecorded = YES;
        self.descNormalH = self.descL.height;
        self.selfNormalH = self.height;
    }
    
    CGFloat duration = .25;
    CGFloat labelRealH = [self getDescRealHeight];  // label的真实高度
    CGFloat addH = labelRealH - self.descNormalH;   // 要增加的高度
    __block CGFloat viewFinalH = self.selfNormalH + addH; //view的最终高度
    if(!self.isSpread){
        [UIView animateWithDuration:duration animations:^{
            if(viewFinalH > MaxViewH){
                viewFinalH = MaxViewH;
                ws.scrollView.scrollEnabled = YES;
            }else{
                ws.scrollView.scrollEnabled = NO;
            }
            ws.height = viewFinalH;
            ws.scBottomMoveDistance     = viewFinalH - ws.selfNormalH;
            ws.scrollBottomC.constant   -= ws.scBottomMoveDistance;
            ws.contentHeightC.constant  = ws.selfNormalH + addH - ws.spreadBtn.height*0.5;
            ws.spreadBottomC.constant   = -(viewFinalH - ws.selfNormalH);
            [ws layoutIfNeeded];
        }];
        
    }else{
        self.scrollView.scrollEnabled = NO;
        [UIView animateWithDuration:duration animations:^{
            ws.height = ws.selfNormalH;
            ws.scrollBottomC.constant   += ws.scBottomMoveDistance;
            ws.scrollView.contentSize   = CGSizeMake(0, 0);
            ws.contentHeightC.constant  = ws.selfNormalH;
            ws.spreadBottomC.constant   = 0;
            [ws layoutIfNeeded];
        }completion:^(BOOL finished) {
            ws.descL.height = ws.descNormalH;
        }];
    }
    self.isSpread = !self.isSpread;
}

#pragma mark - 计算文本真实高度
- (CGFloat)getDescRealHeight{
    CGSize textSize = [self.descL sizeThatFits:CGSizeMake(self.bounds.size.width-self.descLeftC.constant*2, MAXFLOAT)];
    return textSize.height;
}

- (void)layoutSubviews{
    [super layoutSubviews];
}

@end
